package cn.yunyichina.provider.base.service.impl;

import cn.yunyichina.provider.base.dao.PersonalUserOpenidDao;
import cn.yunyichina.provider.iface.entity.PersonalUserOpenid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by JoeZ on 2016/10/31.
 */
@Service("personaluseropenidServiceImpl")
public class PersonalUserOpenidServiceImpl {

    @Autowired
    private PersonalUserOpenidDao personalUserOpenidDao;

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private static Logger log = LoggerFactory.getLogger(PersonalUserOpenidServiceImpl.class);
    
    /**
     * 获取openid对象
     * @param userId
     * @param appId
     * @return 如果没有找到，或者参数为空的，返回null。如果找到多个，返回第一个
     */
    public PersonalUserOpenid getPersonalUserOpenid(String userId,String appId,String openId) {
    	//if (StringUtils.isAllNotEmpty(userId,appId)) {
    		PersonalUserOpenid personalUserOpenid = new PersonalUserOpenid();
    		personalUserOpenid.setAppId(appId);
    		personalUserOpenid.setPerUserId(userId);
			personalUserOpenid.setOpenId(openId);
		//根据条件查询医患关系信息，按使用时间倒叙排序
			List<String> orderColumns = new ArrayList<String>();
			orderColumns.add("UPDATE_TIME");
    		List<PersonalUserOpenid> listPersonalUserOpenid = personalUserOpenidDao.select(personalUserOpenid,orderColumns,false);
    		if (CollectionUtils.isEmpty(listPersonalUserOpenid)) {
    			return null;
    		}
    		else {
    			return listPersonalUserOpenid.get(0);
    		}
    	//}
    	//return null;
    }

	/**
	 * 获取openid对象
	 * @param userId
	 * @param appCode
	 * @return 如果没有找到，或者参数为空的，返回null。如果找到多个，返回第一个
	 */
	public PersonalUserOpenid getPersonalUserOpenidByAppCode(String userId,String appCode,String openId) {
		//if (StringUtils.isAllNotEmpty(userId,appId)) {
		PersonalUserOpenid personalUserOpenid = new PersonalUserOpenid();
		personalUserOpenid.setAppCode(appCode);
		personalUserOpenid.setPerUserId(userId);
		personalUserOpenid.setOpenId(openId);
		//根据条件查询医患关系信息，按使用时间倒叙排序
		List<String> orderColumns = new ArrayList<String>();
		orderColumns.add("UPDATE_TIME");
		List<PersonalUserOpenid> listPersonalUserOpenid = personalUserOpenidDao.select(personalUserOpenid,orderColumns,false);
		if (CollectionUtils.isEmpty(listPersonalUserOpenid)) {
			return null;
		}
		else {
			return listPersonalUserOpenid.get(0);
		}
		//}
		//return null;
	}
}
